
define(function (require, exports, module) {
    require("Firebird.App");
    require("Firebird.Controller");
    require("Format");

    var controller = Firebird.create("Firebird.Controller", {
        controller: "{contextPath}/etl/etlcustomsynctablecfg"
    });

    var controller_tableListSearch = Firebird.create("Firebird.Controller", {
        controller: "{contextPath}/etl/etlcustomsynctablecfg",
        mapping: {"search": "/tableListSearch"}
    });

    var controller_tableListSearch1 = Firebird.create("Firebird.Controller", {
        controller: "{contextPath}/etl/etlcustomsynctablecfg",
        mapping: {"search": "/tableListSearch1"}
    });

    Firebird.define("etlcustomsynctablecfg.SearchApp", {
        extend: "Firebird.SearchApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;

            var formhelper = this.context.J_form;
            /*this.checkComponentsPromise(formhelper, {}, function () {
            var J_grid = context.J_grid = Firebird.create("Firebird.ui.Grid", {
                renderTo: $("#J_grid"),
                formhelper: formhelper,
                controller: controller,
                otherHeight: 160,
                rownumWidth: 35,
                shrinkToFit: true,
                jsonReader: {id: "id"},
                colModel: [
                    {name: "id", hidden: true, width: 1, frozen: true},
                    {name: "connName", label: "连接名称",sortable: false, width: 30, align: "center",
                        formatter: function (val, me, row) {
                            return require("tmpl").compile("<a href='javascript:;' data-id='{{id}}'>{{connName}}</a>")(row);
                        }
                    },
                    {name: "serverAddr", label: "服务器地址",sortable: false, width: 30, align: "center"},
                    {name: "type", label: "数据库类型",sortable: false, width: 30, align: "center"},
                    {name: "name", label: "数据库名称",sortable: false, width: 50, align: "center"},
                    {name: "port", label: "端口号",sortable: false, width: 20, align: "center"},
                    {name: "user", label: "连接账号",sortable: false, width: 20, align: "center"}
                ]
            });
                //J_grid.onAutosize = function () {
                //    J_grid.setGridHeight($(window).height() - 210);
                //}
            });*/
        },
        listener: {
            "click::#J_grid>>a": "view"
        },

        setActionIds: function (e) {
            this.actionIds = typeof $(e.target).data("id") != "undefined" ? [$(e.target).data("id")] : this.context.J_grid.getCheckedRowIds();
            return this;
        }
    });

    Firebird.define("etlcustomsynctablecfg.TableListApp", {
        extend: "Firebird.EditApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            var me = this;
            var context = this.context;
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("多表选择");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);

            context.data = firebird.data.get(firebird.url.getParameter("_FBID_") || window.name) || {};
            context.dialog = _top.$.dialog && _top.$.dialog.get(window) || require("dialog");
            context.dialog && context.dialog.button && context.dialog.button(me.setButtons([
                {
                    value: "下一步",
                    autofocus: true,
                    callback: function () {
                        me.bindNextValidater();
                        return false;
                    }
                },
                {
                    value: "取 消"
                }
            ]));
            this.setValue();
            this.hideProgress();
        },
        bindNextValidater: function (e) {
            var me = this, context = this.context;
            var param = context.J_form.getValue();
            context.J_form.instance.isValid(function (result) {
                if (result == true) {
                    var gridRows = context.J_grid.getCheckedRowIds();
                    if (gridRows.length != 1) {
                        Firebird.ui.Dialog.warn("请从[左边]选择一条数据进行操作。");
                        return;
                    };
                    var grid1Rows = context.J_grid1.getCheckedRowIds();
                    if (grid1Rows.length != 1) {
                        Firebird.ui.Dialog.warn("请从[右边]选择一条数据进行操作。");
                        return;
                    };

                    //设置弹出页面请求地址
                    var action = controller.buildRequest("{contextPath}etl/etlcustomsynctablecfg/jsplumb");
                    //通过uuid，使用firebird.data，建立父子页面之前的上下文关联
                    firebird.data.set(action.uuid, {
                        //定义一个对话框，并存放于上下文中
                        dialog: Firebird.ui.Dialog.open(action.url + "?_FBID_=" + action.uuid, {
                            data: {sourceId: param.sourceId, targetId: param.targetId, savePath: param.savePath},
                            //对话框关闭时触发的事件
                            onclose: function () {
                                //通过this.returnValue，获取对话框返回的值
                                var json = this.returnValue;
                                //根据返回的值处理相关业务，从json.data中获取返回的业务数据
                                if(json.success) {
                                    console.info(json.data);    //打印返回的数据
                                }
                            }
                        })
                    });
                }
            });
        },
        handlers: {

        },
        setValue: function (callback) {
            var me = this;
            var context = this.context;
            context.sourceId.setValue(context.dialog.data.sourceId);
            context.targetId.setValue(context.dialog.data.targetId);
            context.savePath.setValue(context.dialog.data.savePath);
            var formhelper = this.context.J_form;
            var J_grid = context.J_grid = Firebird.create("Firebird.ui.Grid", {
                renderTo: $("#J_grid"),
                controller: controller_tableListSearch,
                formhelper: formhelper,
                otherHeight: 260,
                showPager: false,
                shrinkToFit: true,
                multiboxonly: false,
                jsonReader: {id: "tableId"},
                colModel: [
                    {name: "tableId", hidden: true, width: 1, frozen: true},
                    {name: "tableName",label: "表名",align: "left"}
                ]
            });

            // 初始化右边grid
            var J_grid1 = context.J_grid1 = Firebird.create("Firebird.ui.Grid", {
                renderTo: $("#J_grid1"),
                controller: controller_tableListSearch1,
                formhelper: formhelper,
                otherHeight: 260,
                showPager: false,
                shrinkToFit: true,
                multiboxonly: false,
                jsonReader: {id: "tableId"},
                colModel: [
                    {name: "tableId", hidden: true, width: 1, frozen: true},
                    {name: "tableName",label: "表名",align: "left"}
                ]
            });
        },
        listener: {
            "click::#J_id": "add",
            "click::#J_id1": "addAll",
            "click::#J_id2": "removeAll",
            "click::#J_id3": "remove"
        },
        handlers: {
            add: function (e) {
                console.log("add");
                var app = this, context = this.context;
                // 获取选中表名ID
                var ids = context.J_grid.getCheckedRowIds();
                // 获取选中表名数据
                var rows = context.J_grid.getCheckedRows();
                if (rows.length == 0) {
                    // Firebird.ui.Dialog.warn("请选择一条或多条数据进行操作。");
                    return;
                }
                console.log(rows);

                // 添加行到右边grid
                $("#J_grid1").jqGrid("addRowData", "tableId", rows, "first");
                // 从左边删除已添加行
                ids.forEach(function (v) {
                    $("#J_grid").jqGrid("delRowData", v);
                });
            },
            addAll: function (e) {
                console.log("batchAdd");
                var app = this, context = this.context;
                // 获取所有行
                var rows = context.J_grid.getRawRow();
                if (rows.length == 0) {
                    // Firebird.ui.Dialog.warn("请选择一条或多条数据进行操作。");
                    return;
                }
                // 清除右边grid所有行
                context.J_grid1.clearGridData();
                // 添加行到右边grid
                $("#J_grid1").jqGrid("addRowData", "tableId", rows, "first");
                // 从左边删除已添加行
                rows.forEach(function (v) {
                    $("#J_grid").jqGrid("delRowData", v.tableId);
                });
            },
            removeAll: function (e) {
                console.log("batchRemove");
                var app = this, context = this.context;
                // 获取所有行ID
                var ids = context.J_grid1.getDataIDs();
                // 获取所有行内容
                var rows = [];
                // 获取选中表名数据
                ids.forEach(function (v) {
                    rows.push(context.J_grid1.getRowData(v));
                })
                if (rows.length == 0) {
                    // Firebird.ui.Dialog.warn("请选择一条或多条数据进行操作。");
                    return;
                }
                // 添加行到左边grid
                $("#J_grid").jqGrid("addRowData", "tableId", rows, "first");
                // 从右边删除已添加行
                rows.forEach(function (v) {
                    $("#J_grid1").jqGrid("delRowData", v.tableId);
                });
            },
            remove: function (e) {
                console.log("remove");
                // 获取选中表名ID
                var app = this, context = this.context;
                var ids = context.J_grid1.getCheckedRowIds();
                var rows = [];
                // 获取选中表名数据
                ids.forEach(function (v) {
                    rows.push(context.J_grid1.getRowData(v));
                });
                if (rows.length == 0) {
                    // Firebird.ui.Dialog.warn("请选择一条或多条数据进行操作。");
                    return;
                }

                // 添加行到左边grid
                $("#J_grid").jqGrid("addRowData", "tableId", rows, "first");
                // 从右边删除已添加行
                ids.forEach(function (v) {
                    $("#J_grid1").jqGrid("delRowData", v);
                });
            }
        }
    });

    Firebird.define("etlcustomsynctablecfg.TableColumnListApp", {
        extend: "Firebird.EditApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            var me = this;
            var context = this.context;
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("表连线");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);

            context.data = firebird.data.get(firebird.url.getParameter("_FBID_") || window.name) || {};
            context.dialog = _top.$.dialog && _top.$.dialog.get(window) || require("dialog");
            context.dialog && context.dialog.button && context.dialog.button(me.setButtons([
                {
                    value: "生 成",
                    autofocus: true,
                    callback: function () {
                        me.bindNextValidater();
                        return false;
                    }
                },
                {
                    value: "取 消"
                }
            ]));
            this.setValue();
            this.hideProgress();
        },
        bindNextValidater: function (e) {
            var me = this, context = this.context;
            var param = context.J_form.getValue();
            context.J_form.instance.isValid(function (result) {
                if (result == true) {
                    var gridRows = context.J_grid.getCheckedRowIds();
                    if (gridRows.length != 1) {
                        Firebird.ui.Dialog.warn("请从[左边]选择一条数据进行操作。");
                        return;
                    };
                    var grid1Rows = context.J_grid1.getCheckedRowIds();
                    if (grid1Rows.length != 1) {
                        Firebird.ui.Dialog.warn("请从[右边]选择一条数据进行操作。");
                        return;
                    };

                    //设置弹出页面请求地址
                    var action = controller.buildRequest("{contextPath}etl/etlcustomsynctablecfg/jsplumb");
                    //通过uuid，使用firebird.data，建立父子页面之前的上下文关联
                    firebird.data.set(action.uuid, {
                        //定义一个对话框，并存放于上下文中
                        dialog: Firebird.ui.Dialog.open(action.url + "?_FBID_=" + action.uuid, {
                            data: {sourceId: param.sourceId, targetId: param.targetId, savePath: param.savePath},
                            //对话框关闭时触发的事件
                            onclose: function () {
                                //通过this.returnValue，获取对话框返回的值
                                var json = this.returnValue;
                                //根据返回的值处理相关业务，从json.data中获取返回的业务数据
                                if(json.success) {
                                    console.info(json.data);    //打印返回的数据
                                }
                            }
                        })
                    });
                }
            });
        },
        handlers: {

        },
        setValue: function (callback) {
            var me = this;
            var context = this.context;
            context.sourceId.setValue(context.dialog.data.sourceId);
            context.targetId.setValue(context.dialog.data.targetId);
            context.savePath.setValue(context.dialog.data.savePath);
            var formhelper = this.context.J_form;

        }
    });

    Firebird.define("etlcustomsynctablecfg.SaveApp", {
        extend: "Firebird.SaveApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            var me = this;
            var context = this.context;

            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("同步表向导");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);

            context.data = firebird.data.get(firebird.url.getParameter("_FBID_") || window.name) || {};
            context.dialog = _top.$.dialog && _top.$.dialog.get(window) || require("dialog");
            context.dialog && context.dialog.button && context.dialog.button(me.setButtons([
                {
                    value: "下一步",
                    autofocus: true,
                    callback: function () {
                        me.bindNextValidater();
                        return false;
                    }
                },
                {
                    value: "取 消"
                }
            ]));
            this.hideProgress();
        },
        bindNextValidater: function () {
            var me = this, context = this.context;
            context.J_form.instance.isValid(function (result) {
                if (result == true) {
                    var param = context.J_form.getValue();
                    console.log(param);

                    //设置弹出页面请求地址
                    var action = controller.buildRequest("{contextPath}etl/etlcustomsynctablecfg/tableList");
                    //通过uuid，使用firebird.data，建立父子页面之前的上下文关联
                    firebird.data.set(action.uuid, {
                        //定义一个对话框，并存放于上下文中
                        dialog: Firebird.ui.Dialog.open(action.url + "?_FBID_=" + action.uuid, {
                            title: "多表选择",
                            height: "550",
                            width: "300",
                            data: {sourceId: param.connName, targetId: param.connName1, savePath: param.savePath},
                            //对话框关闭时触发的事件
                            onclose: function () {
                                //通过this.returnValue，获取对话框返回的值
                                var json = this.returnValue;
                                //根据返回的值处理相关业务，从json.data中获取返回的业务数据
                                if(json.success) {
                                    console.info(json.data);    //打印返回的数据
                                }
                            }
                        })
                    });
                }
            });
        },
        handlers: {

        }
    });



    Firebird.define("etlcustomsynctablecfg.EditApp", {
        extend: "Firebird.EditApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("修改数据源信息");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);
        },
        setButtons: function (buttons) {
            buttons[0].value = "保存";
            buttons[1].value = "取消";
            return buttons;
        },
        setValue: function (callback) {
            var me = this;
            var context = this.context;
            me.controller.select(context.data.app.getActionIds()[0], {}, function (json) {
                me.checkComponentsPromise(context.J_form, json.data, function () {
                    if (me.beforeSetValue.apply(me, [context.J_form, json.data]) == true) {
                        context.J_form.setValue(json.data);

                    }
                    me.afterSetValue();
                });
            });
        }
    });



    Firebird.define("etlcustomsynctablecfg.ViewApp", {
        extend: "Firebird.ViewApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("查看数据源信息");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);
        },
        setValue: function (callback) {
            var me = this;
            var context = this.context;
            me.controller.select(context.data.app.getActionIds()[0], {}, function (json) {
                me.checkComponentsPromise(context.J_form, json.data, function () {
                    if (me.beforeSetValue.apply(me, [context.J_form, json.data]) == true) {
                        context.J_form.setValue(json.data);

                    }
                    me.afterSetValue();
                });
            });
        }
    });
});
